<?php
require_once(dirname(dirname(dirname(__FILE__))).'/conn.php');
 require_once(dirname(__FILE__).'/exception.php');
$sqlname="information_schema";
$conn_db_meta=mysql_connect($hostP7, $uP7, $pwdP7."",true);
  $GLOBALS["varsx"]["conn_db_meta"]=$conn_db_meta;
header("Content-Type: text/html; charset=utf-8");
//print_r("msqle:".mysql_error());
//print_r("conn:".$conn);
if(!$conn_db_meta)
throw new Exception("err_login:登录数据错误！"); 


mysql_select_db($sqlname,$conn_db_meta);
if(mysql_error($conn_db_meta))
	throw new Exception(mysql_error($conn_db_meta)); 
//在当前数据库执行SQL语句
mysql_query("set names 'utf8'",$conn_db_meta);
if(mysql_error($conn_db_meta))
	throw new Exception(mysql_error($conn_db_meta)); 
	
function getColsList($tableName,$conn_db_meta)
{
//	SELECT * FROM columns where table_name=&#39;&quot;.$row[&quot;TABLE_NAME&quot;].&quot;&#39; order by COLUMN_NAME
$sql = "SELECT * FROM columns where table_name='".$tableName."' order by COLUMN_NAME";
  $result = mysql_query($sql,$conn_db_meta);  
  	dbExceptionCheck($result);  
	  $GLOBALS["varsx"]["col_trave_after"]=$GLOBALS["col_trave_after"];
	    $row_nums=  mysql_num_rows( $result ) ;
		    $GLOBALS["varsx"]["row_nums"]=$row_nums;
			  $GLOBALS["varsx"]["sql"]=$sql;
		//  print_r($GLOBALS["varsx"]);
		//  echo "get col list after <p>";
		  $a[]=array();
		
  while($row=mysql_fetch_array($result))
  {
	// print_r($row);
	  //col trave after ...event
	  if($GLOBALS["col_trave_after"]!="")
	  {
		
		  $fun=$GLOBALS["col_trave_after"];
		   $a[]= $fun($row);
	  }else	  
	    $a[]=$row["COLUMN_NAME"];
	  
	  
  }
	return $a;
}

function array_grep($a,$keys)
{
$b=array();
foreach ($keys as $key){
$b[$key]=$a[$key];
}
return $b;	
	
}

/*
for insert only
*/
function convert2sql($a,$meta,$conn_db_meta)
{
	  $GLOBALS["varsx"]["meta.. "]=$meta;
	 
	
	//die();
	$meta_obj= json_decode($meta,true);
	$GLOBALS["varsx"]["meta_obj.. "]=$meta_obj;
	
 	$main_key=get_main_key($meta_obj["table"],$conn_db_meta);
	$GLOBALS["varsx"]["main_key.. "]=$main_key;
	$op="insert";
	if($meta_obj["op"]=="merger")
	if($a[$main_key])
		$op="update";
		
	if($op=="insert")
		return getInsertSql($a,	$meta_obj["table"]);
		
		
	
}

/*
for insert only
*/
function getInsertSql($a,	$tab)
{
	$keys=array_keys($a);
	$culs=join(",",$keys);
	$GLOBALS["varsx"]["get parsms "]=$a;
		$GLOBALS["varsx"]["array_keys "]=$keys;
	$GLOBALS["varsx"]["culs "]=$culs;
	//print_r($GLOBALS["varsx"]);
	$vals=join4sqlVal($a);
	$sql="insert INTO ".$tab."(".$culs.") values(".$vals.")";
	return $sql;
}
function join4sqlVal($a)
{
	$v_str="";
	$keys=array_keys($a);
	foreach ($keys as $key){
		if($v_str=="")
			$v_str= "'".$a[$key]."'";
		 else
			 $v_str=$v_str.",'".$a[$key]."'";
	
	}
	return  $v_str;
}
function get_main_key($tab,$conn)
{
	 $GLOBALS["varsx"]["get_main_key..tab"]=$tab;
	
//	die();
	if(!$tab)
 		throw new Exception("get_main_key .\$tab is null" ); 
	//echo "<p>---get_main_key--<p>";
	$GLOBALS["col_trave_after"]=function($colus_row)
	{
			$GLOBALS["col_trave_after_enterx"]="true";
		if($colus_row["COLUMN_KEY"]=="PRI")
			return  $colus_row["COLUMN_NAME"];
		else
			return "";
		
	};
	$a=getColsList($tab,$conn);
	$GLOBALS["col_trave_after"]=null;
	  $GLOBALS["varsx"]["get_main_key..a"]=$a;
	// echo "<p>del event after";
	//print_r($GLOBALS["varsx"]);
	if($a)
	foreach ($a as $item)
	{
		if(!$item)
			return $item;
		
	}	
}

function insert()
{
	global $conn_db_meta;
	$req_keys=array_keys($_GET);// - 获取数组键名

$a=getColsList("wxb_visit_log",$conn_db_meta);
//print_r($a);
//echo "ok";
//print_r($req_keys);
$intersection = array_intersect($a, $req_keys);  
//print_r($intersection);  
$get_greped=array_grep($_GET,$intersection);
//print_r($get_greped);  
  $GLOBALS["varsx"]["get_greped. "]=$get_greped;
$sql=convert2sql($get_greped,$_GET['$meta'],$conn_db_meta);
 $GLOBALS["varsx"]["insert sql:"]=$sql;
global $conn;
//echo $sql;
// print_r($conn);
//die();
$result=mysql_query($sql,$conn);
dbExceptionCheck($result,$conn);

	//print_r( $GLOBALS["varsx"]);
//	die();
}


function insert2()
{
	global $conn_db_meta;
	//print_r($conn_db_meta);
	$req_keys=array_keys($_GET);// - 获取数组键名
$meta= json_decode($_GET['$meta'],true);

$GLOBALS["varsx"]["meta from reg"]=$meta;
$tab= trim(str_replace("`","",$meta["table"])); 
$GLOBALS["varsx"]["tab"]=$tab;
$a=getColsList($tab,$conn_db_meta);

//echo "table cols::";
//print_r($a);echo "-------------";
//echo "ok";
//print_r($req_keys);
$intersection = array_intersect($a, $req_keys);  
//print_r($intersection);  
$get_greped=array_grep($_GET,$intersection);
//print_r($get_greped);  
  $GLOBALS["varsx"]["get_greped. "]=$get_greped;
//$sql=convert2sql($get_greped,$_GET['$meta'],$conn_db_meta);
$sql=getInsertSql($get_greped,	$meta["table"]);
 $GLOBALS["varsx"]["insert sql:"]=$sql;
global $conn;
//echo $sql;
 // print_r( $GLOBALS["varsx"]);die();
//die();
$result=mysql_query($sql,$conn);
dbExceptionCheck($result,$conn);

	//print_r( $GLOBALS["varsx"]);
//	die();
}

function getFlds_str_4query($a)
{
	$b=array();
		foreach ($a as $k=>$v)
	{
		$b[]= $k."='".$v."'";
		
		
	}	
	return  join(" and  ",$b);
	
}
function convert2sql_for_query($a,$tab)
{
 
	$vals=getFlds_str_4query($a);
	$GLOBALS["varsx"]["get parsms "]=$a;
		$GLOBALS["varsx"]["array_keys "]=$keys;
	$GLOBALS["varsx"]["culs "]=$culs;
 
 $where_exp=" where ".$vals;
 if(count($a)==0)
  $where_exp="";
	$sql="select * from  ".$tab.  $where_exp." ";
	
		$GLOBALS["varsx"]["  convert2sql_for_query sql "]=$sql;
	//	print_r($GLOBALS["varsx"]);
	
	return $sql;
}
function convert2sql_for_del($a,$tab)
{ 
	$vals=getFlds_str_4query($a);
	$GLOBALS["varsx"]["get parsms "]=$a;
		$GLOBALS["varsx"]["array_keys "]=$keys;
	$GLOBALS["varsx"]["culs "]=$culs;
	//print_r($GLOBALS["varsx"]);
 // must be where .. for secury reason
	$sql="delete   from  ".$tab." where ".$vals." ";
	
		$GLOBALS["varsx"]["  convert2sql_for_query sql "]=$sql;
	//	print_r($GLOBALS["varsx"]);
	
	return $sql;
}


function    select()
{
	global $conn_db_meta;
	$req_keys=array_keys($_GET);// - 获取数组键名

	$a=getColsList("wxb_visit_log",$conn_db_meta);
	//print_r($a);
	//echo "ok";
	//print_r($req_keys);
	$intersection = array_intersect($a, $req_keys);  
	//print_r($intersection);  
	$get_greped=array_grep($_GET,$intersection);
	//print_r($get_greped);  
	  $GLOBALS["varsx"]["get_greped. "]=$get_greped;
	   $GLOBALS["varsx"]["meta  :"]=$_GET['$meta'];
	  	$meta_obj= json_decode($_GET['$meta'],true);
		 $GLOBALS["varsx"]["meta_obj  :"]=$meta_obj;
	 $tab= $meta_obj["table"];
	$sql=convert2sql_for_query($get_greped, $tab);
	$sql.=	$meta_obj["page"];
	
	 $GLOBALS["varsx"]["select sql:"]=$sql;
	global $conn;
	//echo $sql;
	  print_r($GLOBALS["varsx"]);
	//die();
	$result=mysql_query($sql,$conn);
	dbExceptionCheck($result,$conn);
	
	while($row = mysql_fetch_array($result)) {
		  
			$rowsx[]=$row;
	}
	//$row = mysql_fetch_array($result);
	  $rzt["total"]=100;
		$rzt["rows"]=$rowsx;
	//echo "tt";
//	print_r("aa");
    return  json_encode( $rzt);
	//print_r( $GLOBALS["varsx"]);
//	die();
}

function select2()
{
	global $conn_db_meta;
	$req_keys=array_keys($_GET);// - 获取数组键名

$meta= json_decode($_GET['$meta'],true);
$meta_obj=$meta;
$GLOBALS["varsx"]["meta from reg"]=$meta;
$tab= trim(str_replace("`","",$meta["table"]));
	$a=getColsList($tab,$conn_db_meta);
$a=addAllLowcaseKey($a);
	// print_r($a);
	//echo "ok";
	//print_r($req_keys);
	$intersection = array_intersect($a, $req_keys);  
	//print_r($intersection);  
	$get_greped=array_grep($_GET,$intersection);
	//print_r($get_greped);  
	  $GLOBALS["varsx"]["get_greped. "]=$get_greped;
	   $GLOBALS["varsx"]["meta  :"]=$_GET['$meta'];
	  	$meta_obj= json_decode($_GET['$meta'],true);
		 $GLOBALS["varsx"]["meta_obj  :"]=$meta_obj;
	 $tab= $meta_obj["table"];
	$sql=convert2sql_for_query($get_greped, $tab);
	if($meta_obj["orderby"])
	$sql.= " order by ". 	$meta_obj["orderby"];
	$sql.=	$meta_obj["page"];
	
	 $GLOBALS["varsx"]["select sql:"]=$sql;
	global $conn;
	//echo $sql;
	//  print_r($GLOBALS["varsx"]);
	//die();
	$result=mysql_query($sql,$conn);
	dbExceptionCheck($result,$conn);
	
	while($row = mysql_fetch_array($result)) {
		  
			$rowsx[]=$row;
	}
	//$row = mysql_fetch_array($result);
	  $rzt["total"]=100;
		$rzt["rows"]=$rowsx;
	//echo "tt";
//	print_r("aa");
    return  json_encode( $rzt);
	//print_r( $GLOBALS["varsx"]);
//	die();
}


function delete()
{
	global $conn_db_meta;
	$req_keys=array_keys($_GET);// - 获取数组键名

$meta= json_decode($_GET['$meta'],true);
$meta_obj=$meta;
$GLOBALS["varsx"]["meta from reg"]=$meta;
$tab= trim(str_replace("`","",$meta["table"]));
	$a=getColsList($tab,$conn_db_meta);
$a=addAllLowcaseKey($a);
	// print_r($a);
	//echo "ok";
	//print_r($req_keys);
	$intersection = array_intersect($a, $req_keys);  
	//print_r($intersection);  
	$get_greped=array_grep($_GET,$intersection);
	//print_r($get_greped);  
	  $GLOBALS["varsx"]["get_greped. "]=$get_greped;
	   $GLOBALS["varsx"]["meta  :"]=$_GET['$meta'];
	  	$meta_obj= json_decode($_GET['$meta'],true);
		 $GLOBALS["varsx"]["meta_obj  :"]=$meta_obj;
	 $tab= $meta_obj["table"];
	$sql=convert2sql_for_del($get_greped, $tab);
	 
	
	 $GLOBALS["varsx"]["  sql:"]=$sql;
	global $conn;
	//echo $sql;
	//  print_r($GLOBALS["varsx"]);
	//die();
	$result=mysql_query($sql,$conn);
	dbExceptionCheck($result,$conn);
	
	//while($row = mysql_fetch_array($result)) {
//		  
//			$rowsx[]=$row;
//	}
	//$row = mysql_fetch_array($result);
	  $rzt["total"]=$result;
		$rzt["rows"]=$rowsx;
	//echo "tt";
//	print_r("aa");
    return  json_encode( $rzt);
	//print_r( $GLOBALS["varsx"]);
//	die();
}

function addAllLowcaseKey($a)
{
	//print_r($a);
	foreach ($a as $key){
		$newKey=strtolower($key);
		$a[]=$newKey;
	}
	//print_r($a);die();
	return $a;
	
}


function select_by_sql_singleValue()
{
	
	$sql=$_GET["sql"];
	global $conn;
	$result=mysql_query($sql,$conn);
//	dbExceptionCheck($result,$conn);
	
	 $row = mysql_fetch_array($result);
	 return $row[0];
}

?>
